package com.auxgroup.smarthome.init;

import com.auxgroup.smarthome.event.DeviceFaultEvent;
import com.auxgroup.smarthome.event.OfflineEvent;
import com.auxgroup.smarthome.event.OnlineEvent;
import com.auxgroup.smarthome.event.SmartElectricityEvent;
import com.auxgroup.smarthome.snoti.Setting;
import com.auxgroup.smarthome.snoti.client.NotiBootrap;
import com.gizwits.noti2.client.Events;
import com.gizwits.noti2.client.LoginData;
import com.gizwits.noti2.client.PushEvents;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 初始化snoti连接
 * Created by lixiaoxiao on 17-8-8.
 */
@Component
public class InitSnoti {
    @Autowired
    private OnlineEvent onlineEvent;
    @Autowired
    private OfflineEvent offlineEvent;
    @Autowired
    private DeviceFaultEvent deviceFaultEvent;
    @Autowired
    private SmartElectricityEvent smartElectricityEvent;

    public void initConnectSnoti() {
        String productKeyHome = Setting.getValue("gizwits.productKeyHome").trim();
        String productKeyBusiness = Setting.getValue("gizwits.productKeyBusiness").trim();
        String authIdHome = Setting.getValue("gizwits.productKeyHome.authId").trim();
        String authIdBusiness = Setting.getValue("gizwits.productKeyBusiness.authId").trim();
        String authSecretHome = Setting.getValue("gizwits.productKeyHome.authSecret").trim();
        String authSecretBusiness = Setting.getValue("gizwits.productKeyBusiness.authSecret").trim();
        NotiBootrap notiBootrap = new NotiBootrap();
        LoginData loginHomeData = new LoginData(productKeyHome, authIdHome, authSecretHome,
                "client_home", Arrays.asList(Events.ONLINE, Events.OFFLINE, Events.ATTR_FAULT, Events.STATUS_KV));
        LoginData loginBusinessData = new LoginData(productKeyBusiness, authIdBusiness, authSecretBusiness,
                "client_commerce", Arrays.asList(Events.ONLINE, Events.OFFLINE, Events.ATTR_FAULT, Events.STATUS_KV));
        List<LoginData> datas = new ArrayList<>();
        datas.add(loginHomeData);
        datas.add(loginBusinessData);
        notiBootrap.loginNoti(datas);
        notiBootrap.registerCallbackEvent(PushEvents.DEVICE_ONLINE, onlineEvent);
        notiBootrap.registerCallbackEvent(PushEvents.DEVICE_OFFLINE, offlineEvent);
        notiBootrap.registerCallbackEvent(PushEvents.ATTR_FAULT, deviceFaultEvent);
        notiBootrap.registerCallbackEvent(PushEvents.DEVICE_STATUS_KV, smartElectricityEvent);
        notiBootrap.receiveMessage();
    }
}